package com.ruoyi.web.controller.totalinventory;

import java.util.List;

import com.ruoyi.domain.ThisStock;
import com.ruoyi.service.IThisStockService;
import com.ruoyi.system.domain.ProCreateBuilding;
import com.ruoyi.system.service.IProCreateBuildingService;
import net.bytebuddy.asm.Advice;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.TotalInventory;
import com.ruoyi.service.ITotalInventoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-07-30
 */
@Controller
@RequestMapping("/ruoyi/inventory")
public class TotalInventoryController extends BaseController
{
    private String prefix = "ruoyi/inventory";

    @Autowired
    private ITotalInventoryService totalInventoryService;

    @Autowired
    private IThisStockService thisStockService;

    @Autowired
    private IProCreateBuildingService proCreateBuildingService;

    @RequiresPermissions("ruoyi:inventory:view")
    @GetMapping()
    public String inventory()
    {
        return prefix + "/inventory";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:inventory:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TotalInventory totalInventory)
    {

        ProCreateBuilding pb = new ProCreateBuilding();
        TotalInventory totalInventory1=new TotalInventory();
        List<ProCreateBuilding> li = proCreateBuildingService.selectProCreateBuildingList(pb);
        for(int j=0;j<li.size();j++){
            if(li.get(j).getpState()==2){
                ThisStock ts = new ThisStock();
                List<ThisStock> lists = thisStockService.selectThisStockLists(li.get(j).getpId());
                for (int i=0;i<lists.size();i++){
                    totalInventory1.settName(lists.get(i).gettName());
                    totalInventory1.settNum(lists.get(i).gettNum());
                    totalInventoryService.insertTotalInventory(totalInventory1);
                    thisStockService.deleteThisStockById(lists.get(i).getId());
                }
            }
        }
        startPage();
        List<TotalInventory> list = totalInventoryService.selectTotalInventoryList(totalInventory);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("ruoyi:inventory:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TotalInventory totalInventory)
    {
        List<TotalInventory> list = totalInventoryService.selectTotalInventoryList(totalInventory);
        ExcelUtil<TotalInventory> util = new ExcelUtil<TotalInventory>(TotalInventory.class);
        return util.exportExcel(list, "inventory");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:inventory:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TotalInventory totalInventory)
    {
        return toAjax(totalInventoryService.insertTotalInventory(totalInventory));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TotalInventory totalInventory = totalInventoryService.selectTotalInventoryById(id);
        mmap.put("totalInventory", totalInventory);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:inventory:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TotalInventory totalInventory)
    {
        return toAjax(totalInventoryService.updateTotalInventory(totalInventory));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("ruoyi:inventory:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(totalInventoryService.deleteTotalInventoryByIds(ids));
    }
}
